<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
	<link rel="shortcut icon" href="/static/img/favicon.ico">
	<link rel="stylesheet" href="/static/css/bootstrap.min.css"/>
	<title>login</title>
</head>
<body>
	<script type="text/javascript" src="/static/js/jquery.min.js"></script>
	<script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
	<div style="text-align:center;margin-top:100px;">
		<form id="loginForm">
			<input id="login_phone" type="text" name="phone" placeholder="手机号"><br>
			<input id="login_password" type="password" name="password" placeholder="密码"><br>
			<img id="login_img" src="" width="120" height="30" alt="验证码"/>
			<input id="login_code" type="text" name="code" placeholder="验证码"><br>
			<input id="login_verificatcode" type="hidden" name="verificatcode">
			<input id="login_remember" type="checkbox" name="remember">7天内免登录</input><br>
			<button id="login_submit" onclick="login();" type="button">登陆</button><br>
			<a onclick="changeLRPage();" href="javascript:;">没账号?去注册...</a>
		</form>
		<form id="registerForm" style="display:none;">
			<input id="register_username" type="text" name="username" placeholder="用户名"><br>
			<input id="register_phone" type="text" name="phone" placeholder="手机号"><br>
			<input id="register_password" type="password" name="password" placeholder="密码"><br>
			<input id="register_password2" type="password" placeholder="请再输入密码"><br>
			<input id="register_invitationcode" type="text" name="invitationcode" placeholder="邀请码"><br>
			<img id="register_img" src="" width="120" height="30" alt="验证码"/>
			<input id="register_code" type="text" name="code" placeholder="验证码"><br>
			<input id="register_verificatcode" type="hidden" name="verificatcode">
			<button id="register_submit" onclick="register();" type="button">注册</button><br>
			<a onclick="changeLRPage();" href="javascript:;">有账号?去的登录...</a>
		</form>
	</div>
	
	<script type="text/javascript">
		var URL = "http://"+window.location.host;
		var TOKEN = "";
		function getCode(){
			$.ajax({
				url: URL+"/code",
				type: 'GET',
				data: null,
				dataType: 'JSON',
				success:function(result){
					if(result.code == 0){
						$("#login_verificatcode").val(result.msg);
						$("#login_img").attr("src",result.data);
						$("#register_verificatcode").val(result.msg);
						$("#register_img").attr("src",result.data);
					}else{
						alert("获取验证码出错");
					}
				},
				error:function(){alert("获取验证码出错")}
			});
		}
		getCode();
		$("#login_img").click(function(){
			getCode();
		})
		$("#register_img").click(function(){
			getCode();
		})
		function login(){
			var post_data = $("#loginForm").serialize();
			$.ajax({
				url: URL+"/loginverify",
				type: 'POST',
				data: post_data,
				dataType: 'JSON',
				beforeSend: function(request){
					request.setRequestHeader("token",TOKEN);
				},
				success:function(result){
					TOKEN = result.data;
					if(result.code == 0){
						//goIndex();
						window.location.replace("/");
					}else if(result.code == 11){
						getCode();
						alert("验证码错误");
					}else{
						alert(result.msg);
					}
				},
				error:function(){
					alert("用户名或密码格式有误");
				}
			});
			
		}
		function register(){
			var post_data = $("#registerForm").serialize();
			$.ajax({
				url: URL+"/register",
				type: 'POST',
				data: post_data,
				dataType: 'JSON',
				success:function(result){
					if(result.code == 0){
						alert(result.msg);
					}else if(result.code == 11){
						getCode();
						alert("验证码错误");
					}else{
						alert(result.msg);
					}
				},
				error:function(){
					alert("用户名或密码格式有误");
				}
			});
		}
		function goIndex(){
			$.ajax({
				url: URL+"/index",
				type: 'GET',
				dataType: 'JSON',
				beforeSend: function(request){
					request.setRequestHeader("token",TOKEN);
				},
				success:function(result){
					alert(result.msg);
					window.location.replace("/");
				},
				error:function(){
					alert("用户名或密码格式有误");
				}
			});
		}
		function changeLRPage(){
			$("#loginForm").toggle();
			$("#registerForm").toggle();
			getCode();
		}
	</script>
</body>
</html>